package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 固定资产购置-计量器具流程调用SAP的MIGO过账接口（MM021）
 * @ModifyLog:
 **/
public class WeaGdzcgzJlqjlcMIGOAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaGdzcgzJlqjlcMIGOAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String swdm = Util.null2String(rs.getString("swdm"));
            String pzzdpzrq = Util.null2String(rs.getString("pzzdpzrq"));
            String pzzdgzrq = Util.null2String(rs.getString("pzzdgzrq"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("SERIAL", requestid);
            headerJson.put("TCODE", swdm);
            headerJson.put("DOC_DATE", pzzdpzrq.replace("-", ""));
            headerJson.put("PSTNG_DATE", pzzdgzrq.replace("-", ""));
            headerJson.put("HEADER_TXT", lcbh);
            JSONArray itemList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject itemJson = new JSONObject();
                itemJson.put("SERIAL", Util.null2String(rs.getString("id")));
                itemJson.put("SERIALIT", Util.null2String(rs.getString("cgsqdxmbh")));
                itemJson.put("MATERIAL", Util.null2String(rs.getString("wlbh")));
                itemJson.put("MAKTX", Util.null2String(rs.getString("wlms")));
                itemJson.put("BWTAR", Util.null2String(rs.getString("pglx")));
                itemJson.put("PLANT", Util.null2String(rs.getString("gc")));
                itemJson.put("ENTRY_UOM", Util.null2String(rs.getString("cgsqjldw")));
                itemJson.put("ENTRY_QNT", Util.null2String(rs.getString("cgsqsl")));
                itemJson.put("COSTCENTER", Util.null2String(rs.getString("cbzx")));
                itemJson.put("MOVE_TYPE", Util.null2String(rs.getString("ydlxkcgl")));
                itemJson.put("PO_NUMBER", Util.null2String(rs.getString("cgpzbh")));
                itemJson.put("PO_ITEM", Util.null2String(rs.getString("cgpzdxmbh")));
                itemJson.put("DMBTR", Util.null2String(rs.getString("abwbjdje")));
                itemJson.put("PS_POSID", Util.null2String(rs.getString("gzfjjgyswbsys")));
                itemList.add(itemJson);
            }
            headerJson.put("DETAIL", itemList);
            dataJson.put("HEADER", headerJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaGdzcgzJlqjlcMIGOAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaGdzcgzJlqjlcMIGOAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = sapUtil.handleOKHttpRequest("MM073", dataJson, dataMap);
            writeLog(">>>>>WeaGdzcgzJlqjlcMIGOAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equalsIgnoreCase(msgType)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    JSONArray returnList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnList.size(); i++) {
                        String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                        if ("E".equalsIgnoreCase(msgType2)) {
                            String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT");
                            message += msgText2 + ";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaGdzcgzJlqjlcMIGOAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}